package com.gzhryc.pay.adaptation.alipay.msg.advance_pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.gzhryc.common.NumberTools;
import com.gzhryc.common.json.JsonTools;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Map;

public class AlipayAdvancePayConfirmNotify implements Serializable {

    private static final long serialVersionUID = 1L;

    final Map<String, String> params;
    final String payKey;
    final String appId;

    public AlipayAdvancePayConfirmNotify(Map<String, String> params, String payKey,String appId) {
        this.params = params;
        this.payKey = payKey;
        this.appId = appId;
    }

    public String getTradeNo() {
        return this.params.get("out_order_no");
    }

    public String getExternalTradeNo() {
        return this.params.get("auth_no");
    }

    public String getPayerId() {
        return this.params.get("payer_user_id");
    }

    public Integer getMoney() {
        String pre_auth_type = this.params.get("pre_auth_type");
        String credit_amount;
        int money;
        if ("CREDIT_AUTH".equals(pre_auth_type)) {
            credit_amount = this.params.get("credit_amount");
            if (StringUtils.isNotBlank(credit_amount)) {
                money = NumberTools.changeMoney(Double.valueOf(credit_amount));
                return money;
            }
        } else {
            credit_amount = this.params.get("total_freeze_amount");
            if (StringUtils.isNotBlank(credit_amount)) {
                money = NumberTools.changeMoney(Double.valueOf(credit_amount));
                return money;
            }
        }
        return null;
    }

    public String getStatus() {
        return this.params.get("status");
    }

    public boolean isTrue() {
        if (StringUtils.isNotBlank(this.payKey)) {
            try {
                return AlipaySignature.rsaCheckV1(this.params, this.payKey, "UTF-8", "RSA2");
            } catch (AlipayApiException var2) {
                var2.printStackTrace();
            }
        }

        return false;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public String getJson() {
        return JsonTools.toJson(this.params);
    }

	public String getAppId() {
		return appId;
	}
   
}
